// layui.use(['form', 'element', 'util', 'carousel', 'laypage', 'layer', 'table'], function () {
//     var element = layui.element;
//     var util = layui.util;
//     var form = layui.form;
//     var laypage = layui.laypage
//         , layer = layui.layer;
//     form.on('select(types)', function (data) {
//         var indexGID = data.elem.selectedIndex;
//         lookallproduct(data.elem[indexGID].title);
//     });
// });

layui.use(['form', 'element', 'util', 'carousel', 'laypage', 'layer','table'], function () {
    var table = layui.table, form=layui.form;

    table.render({
        elem: '#product'
        , url: '/admin/getproductlist/'
        , page: {
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
            , groups: 3
            , limits: [20, 50, 100]
            , limit: 20
        }, cols: [[
            {field: 'productId', title: 'ID',width:80, align:'center'}
            , {field: 'name', title: '商品名称', width: 180, align:'center'}
            , {field: 'categoryId', title: '商品类别', width: 100, align:'center'}
            , {field: 'description', title: '商品描述', width: 200, align:'center'}
            , {field: 'price', title: '商品价格', width: 100, align:'center'}
            , {field: 'amount', title: '库存', width: 100, align:'center'}
            , {field: 'createTimeStr', title: '上架时间', width: 160,sort: true, align:'center'}
            , {field: 'state', title: '状态', width: 100, align:'center'}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width:200, align:'center'}
        ]], done: function (res, curr, count) {
            $("[data-field='state']").children().each(function () {
                if($(this).text() == 0) {
                    $(this).text("待审核")
                }else if($(this).text()==1){
                    $(this).text("在售")
                }else if($(this).text()==2){
                    $(this).text("下架")
                }
            });
            $("[data-field='categoryId']").children().each(function () {
                if($(this).text() == 1) {
                    $(this).text("手机")
                }else if($(this).text()==2){
                    $(this).text("数码")
                }else if($(this).text()==3){
                    $(this).text("包表首饰")
                }else if($(this).text()==4){
                    $(this).text("游戏装备")
                }else if($(this).text()==5){
                    $(this).text("家用电器")
                }
            });

            // var i=1;
            // $("[data-field='productId']").children().each(function () {
            //     if($(this).text() == 'ID') {
            //         $(this).text("ID")
            //     }else{
            //         $(this).text(i++)
            //     }
            // });
        }
    });
    //监听行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event === 'xiangqing') {
            window.open(basePath+"/front/getproduct/"+data.productId)
        }else if(obj.event === 'weigui'){
            layer.confirm('确认商品违规吗？', {
                btn: ['确定','算了'], //按钮
                title:"违规商品",
                offset:"50px"
            }, function(){
                layer.closeAll();
                $.ajax({
                    url: basePath+'/admin/changecommstatus/'+data.productId+"/0",
                    data: "",
                    contentType: "application/json;charset=UTF-8", //发送数据的格式
                    type: "put",
                    dataType: "json", //回调
                    beforeSend: function () {
                        layer.load(1, { //icon支持传入0-2
                            content: '请稍等...',
                            success: function (layero) {
                                layero.find('.layui-layer-content').css({
                                    'padding-top': '39px',
                                    'width': '60px'
                                });
                            }
                        });
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    },
                    success: function (data) {
                        console.log(data)
                        if(data.code===2){
                            layer.msg(data.message, {
                                time: 1000,
                                icon: 1,
                                offset: '50px'
                            }, function () {
                                location.reload();
                            });
                        }else {
                            layer.msg(data.message, {
                                time: 1000,
                                icon: 2,
                                offset: '50px'
                            });
                        }
                    }
                });
            }, function(){
            });
        }else if (obj.event === 'shenhe') {
            layer.confirm('确认该商品通过审核吗？', {
                btn: ['确定','算了'], //按钮
                title:"审核商品",
                offset:"50px"
            }, function(){
                layer.closeAll();
                $.ajax({
                    url: basePath+'/admin/changecommstatus/'+data.productId+"/1",
                    data: "",
                    contentType: "application/json;charset=UTF-8", //发送数据的格式
                    type: "put",
                    dataType: "json", //回调
                    beforeSend: function () {
                        layer.load(1, { //icon支持传入0-2
                            content: '请稍等...',
                            success: function (layero) {
                                layero.find('.layui-layer-content').css({
                                    'padding-top': '39px',
                                    'width': '60px'
                                });
                            }
                        });
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    },
                    success: function (data) {
                        console.log(data)
                        if(data.code===1){
                            layer.msg(data.message, {
                                time: 1000,
                                icon: 1,
                                offset: '50px'
                            }, function () {
                                location.reload();
                            });
                        }else {
                            layer.msg(data.message, {
                                time: 1000,
                                icon: 2,
                                offset: '50px'
                            });
                        }
                    }
                });
            }, function(){
            });
        }
    });

    //获取条件下拉框中的值
    form.on('select',function (data) {
        console.log(data.value);
        var state=data.value;
        table.reload('product',{
            method:'post',
            url:'/admin/getProductlistBycondition',
            where:{
                state:state,
            }
            ,page:{
                curr:1
            }
        })
        return false
    })

});

// function lookallproduct(stuatus) {
// }
// lookallproduct(100);